package design;

/**
 * Created with IntelliJ IDEA.
 * Description: 单例模式
 * User: Li_yizYa
 * Date: 2025/8/29
 * Time: 20:07
 */

/**
 * 饿汉模式
 */
//public class Singleton {
//    private static final Singleton instance = new Singleton();
//
//    private Singleton() {
//
//    }
//
//    public Singleton getInstance(){
//        return instance;
//    }
//}

//public class Singleton {
//    private static Singleton instance;
//
//    private Singleton(){}
//
//    public synchronized static Singleton getInstance() {
//        if (instance == null) {
//            instance = new Singleton();
//        }
//        return instance;
//    }
//}

public class Singleton {

    private volatile static Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}